﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

/**/
/// <summary>
/// 文件上传类
/// </summary>
public class UploadFile
{
    public static string[] allowedExtensions = { ".doc", ".xls", ".docx", ".xlsx", ".ppt", ".pdf", ".rar", ".zip", ".7z" };

    public UploadFile()
    {

    }

    /// <summary>
    /// 保存文件到服务器
    /// </summary>
    /// <param name="_pathname">服务器的路径（不包含文件名）</param>
    /// <param name="_filename">服务器上保存的文件名（不包含扩展名）</param>
    /// <param name="_fileupload">上传的控件</param>
    /// <param name="MSG">返回的消息</param>
    /// <returns>返回保存成功与否</returns>
    public static bool ExecUploadFile(string _pathname, string _filename, FileUpload _fileupload, out string MSG)
    {
        MSG = "";
        bool returnValue = false;

        bool fileOK = false;
        //获取基于应用程序根文件夹的绝对路径

        //如果确认了上传文件，则判断文件的类型是否符合要求
        if (_fileupload.HasFile)//如果有文件
        {
            string fileExtension = System.IO.Path.GetExtension(_fileupload.FileName).ToLower();
            //判断扩展名是否符合
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                    break;
                }
            }
        }

        if (fileOK)
        {
            try
            {
                if (!Directory.Exists(_pathname))  // 判断当前目录是否存在。
                {
                    Directory.CreateDirectory(_pathname);  // 建立上传文件存放目录。
                }


                _fileupload.SaveAs(_pathname + _filename);
                returnValue = true;
            }
            catch (Exception ex)
            {
                MSG = "上传文件系统错误。错误信息：" + ex.Message;
                returnValue = false;
            }
        }
        else
        {
            returnValue = false;
            MSG = "上传的文件格式不正确！";
        }

        return returnValue;
    }
    
    /// <summary>
    /// 执行文件上传
    /// </summary>
    /// <param name="_fileupload">上传的控件</param>
    /// <param name="_fileData">返回的文件内容数据</param>
    /// <param name="MSG">返回的信息</param>
    /// <returns></returns>
    public static bool ExecUploadFile(FileUpload _fileupload, out byte[] _fileData, out string MSG)
    {
        MSG = "";
        bool returnValue = false;
        _fileData = new byte[] { 0 };

        bool fileOK = false;
        //获取基于应用程序根文件夹的绝对路径

        //如果确认了上传文件，则判断文件的类型是否符合要求
        if (_fileupload.HasFile)//如果有文件
        {
            //判断扩展名是否符合
            string fileExtension = System.IO.Path.GetExtension(_fileupload.FileName).ToLower();
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                    break;
                }
            }
        }

        if (fileOK)
        {
            try
            {
                Stream fileDataStream = _fileupload.PostedFile.InputStream;
                HttpPostedFile file = _fileupload.PostedFile;
                int fileLength = _fileupload.PostedFile.ContentLength;

                //将文件内容赋值到字节数组
                _fileData = new byte[fileLength];

                //把文件流填充到数组  
                fileDataStream.Read(_fileData, 0, fileLength);

                returnValue = true;
            }
            catch (Exception ex)
            {
                MSG = "上传文件系统错误。错误信息：" + ex.Message;
                returnValue = false;
            }
        }
        else
        {
            returnValue = false;
            MSG = "上传的文件格式不正确！";
        }

        return returnValue;
    }
}